package com.haha.extend.EditText;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.EditText;

import com.Long.library.R;


/**
 * Created by Administrator on 2016/6/21 0021.
 */
public class UnderLineEditText extends EditText{

    private static final int LINE_TYPE_STRAIGHT_LINE = 1;
    private static final int LINE_TYPE_WAVE_LINE = 2;

    private static final int LINE_STYLE_DASH = 1;
    private static final int LINE_STYLE_SOLID = 2;

    private Paint linePaint;
    private int underLineColor = Color.GRAY;
    private int underLineHeight = 1;
    private int lineHeightOffset = 0;
    private int lineType = LINE_TYPE_STRAIGHT_LINE;
    private int lineStyle = LINE_STYLE_SOLID;

    private float waveLineStartX;
    private float waveLineRadius = 10f;
    private float waveSpeed = 2f;

    private float delta;

    public UnderLineEditText(Context context) {
        this(context,null);
    }

    public UnderLineEditText(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public UnderLineEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        lineHeightOffset = (int)(0.19753087f * getLineHeight() + 0.5f);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.UnderLineEditText);
        underLineColor = ta.getColor(R.styleable.UnderLineEditText_underLineColor,underLineColor);
        underLineHeight = ta.getDimensionPixelSize(R.styleable.UnderLineEditText_underLineHeight,underLineHeight);
        lineHeightOffset = ta.getDimensionPixelSize(R.styleable.UnderLineEditText_lineHeightOffset,lineHeightOffset);
        lineType = ta.getInt(R.styleable.UnderLineEditText_lineType,lineType);
        lineStyle = ta.getInt(R.styleable.UnderLineEditText_lineStyle,lineStyle);
        waveLineRadius = ta.getDimension(R.styleable.UnderLineEditText_waveLineRadius,waveLineRadius);
        waveSpeed = ta.getFloat(R.styleable.UnderLineEditText_waveSpeed,waveSpeed);
        ta.recycle();

        delta = (float) (Math.sqrt(2) / 2 * waveLineRadius);
        waveLineStartX = -4 * delta;

        linePaint = new Paint();
        linePaint.setStrokeWidth(underLineHeight);
        linePaint.setColor(underLineColor);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setAntiAlias(true);
        if(lineStyle == LINE_STYLE_DASH){
            PathEffect pathEffect = new DashPathEffect(new float[]{5,5,5,5},1);
            linePaint.setPathEffect(pathEffect);
        }
        new WaveMoveThread().start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int lineCount = getLineCount();
        int height = getHeight();
        int lineHeight = getLineHeight();
        lineCount = Math.max(lineCount,1 + height / lineHeight);
        int lineY = getCompoundPaddingTop() + lineHeightOffset;

        canvas.clipRect(getCompoundPaddingLeft(),getCompoundPaddingTop(),getWidth() - getCompoundPaddingRight(),getHeight() - getCompoundPaddingBottom());

        for (int i = 0;i <= lineCount; i++) {
            lineY += lineHeight;
            if(lineType == LINE_TYPE_STRAIGHT_LINE){
                Path path = new Path();
                path.moveTo(getCompoundPaddingLeft(), lineY);
                path.lineTo(getWidth() - getCompoundPaddingRight(), lineY);
                canvas.drawPath(path, this.linePaint);
            } else if(lineType == LINE_TYPE_WAVE_LINE){
                drawWaveLine(canvas,waveLineStartX, lineY,getWidth() - getCompoundPaddingRight(), waveLineRadius,linePaint);
            }
        }
    }




    private void drawWaveLine(Canvas canvas,float startX,float startY,float endX,float radius,Paint paint){
        float x1 = Math.min(startX,endX);
        float x2 = Math.max(startX,endX);
        int n = (int) ((x2 - x1) / delta / 2 + 0.99f) + 1;
        float curX,curY,curStartAngle;
        for(int i = 0; i < n; i++){
            curX = x1 + (2 * i + 1) * delta;
            curY = startY + (i % 2 == 0 ? delta : -delta);
            curStartAngle = i % 2 == 0 ? 225f : 45f;
            drawArc(canvas,curX,curY,radius,curStartAngle,90f,paint);
        }
    }

    private void drawArc(Canvas canvas,float originX,float originY,float radius,float startAngle,float sweepAngle,Paint paint){
        RectF rectF = new RectF(originX - radius,originY - radius,originX + radius,originY + radius);
        canvas.drawArc(rectF,startAngle,sweepAngle,false,paint);
    }


    private class WaveMoveThread extends Thread{
        private float periodWidth;
        private float waveDistance;
        private float startX;
        private int[] colors = {Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW,Color.GRAY,Color.BLUE};
        private int colorIndex;

        @Override
        public synchronized void start() {
            super.start();
            periodWidth = (float) (waveLineRadius * 2 * Math.sqrt(2));
            waveDistance = 0;
            startX = -4 * delta;
            colorIndex = 0;
        }

        @Override
        public void run() {
            while(lineType == LINE_TYPE_WAVE_LINE){
                try {
                    Thread.sleep(100);
                    if(waveDistance + waveSpeed >= periodWidth) waveDistance = 0;
                    else waveDistance += waveSpeed;
                    waveLineStartX = waveDistance + startX;
                    linePaint.setColor(colors[colorIndex++ % colors.length]);
                    postInvalidate();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
